package com.ysy.tz.mfg.component;

import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.event.BizEvent;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;

public class PigGradingPane extends Pane {
    private IntegerProperty rowCount = new SimpleIntegerProperty(1);
    private IntegerProperty columnCount = new SimpleIntegerProperty(6);
    private IntegerProperty hgap = new SimpleIntegerProperty(5);
    private IntegerProperty vgap = new SimpleIntegerProperty(5);
    private PigGradingAction action = null;
    private LinkedHashMap<Integer, PigGradingAction> actionMap = new LinkedHashMap();
    private PigGradingNode selectNode = null;
    private Pane backPane = new Pane() {
        protected void layoutChildren() {
            PigGradingPane.this.columnCount.set((int)(ApplicationContext.getScreenBounds().getWidth() / 128.0));
            double width = (this.getWidth() - (double)PigGradingPane.this.hgap.get()) / (double)PigGradingPane.this.columnCount.get() - (double)PigGradingPane.this.hgap.get();
            double height = (this.getHeight() - (double)PigGradingPane.this.vgap.get()) / (double)PigGradingPane.this.rowCount.get() - (double)PigGradingPane.this.vgap.get();
            //int colIndexx = false;
            Set<Map.Entry<Integer, PigGradingAction>> entrySet = PigGradingPane.this.actionMap.entrySet();
            int index = 0;
            Iterator var8 = entrySet.iterator();

            while(var8.hasNext()) {
                Map.Entry<Integer, PigGradingAction> actionEntry = (Map.Entry)var8.next();
                int location = (Integer)actionEntry.getKey();
                int rowIndex = location / PigGradingPane.this.columnCount.get();
                PigGradingNode node = (PigGradingNode)this.getChildren().get(index++);
                node.setEmpty(true);
                node.setActionProperty((PigGradingAction)actionEntry.getValue());
                node.prefWidth(width);
                node.prefWidth(height);
                int colIndex = location;
                if (location % PigGradingPane.this.columnCount.get() == 0) {
                    colIndex = 0;
                }

                if (node != null) {
                    node.resize(width, height);
                    node.relocate((double)PigGradingPane.this.hgap.get() + (double)(colIndex++) * (width + (double)PigGradingPane.this.hgap.get()), (double)PigGradingPane.this.vgap.get() + (double)rowIndex * (height + (double)PigGradingPane.this.vgap.get()));
                }
            }

        }
    };
    private Pane contentPane = new Pane() {
        protected void layoutChildren() {
            PigGradingPane.this.columnCount.set((int)(ApplicationContext.getScreenBounds().getWidth() / 128.0));
            double width = (this.getWidth() - (double)PigGradingPane.this.hgap.get()) / (double)PigGradingPane.this.columnCount.get() - (double)PigGradingPane.this.hgap.get();
            double height = (this.getHeight() - (double)PigGradingPane.this.vgap.get()) / (double)PigGradingPane.this.rowCount.get() - (double)PigGradingPane.this.vgap.get();
            int colIndex = 0;

            for(int i = 0; i < this.getChildren().size(); ++i) {
                int rowIndex = i / PigGradingPane.this.columnCount.get();
                Region node = (Region)this.getChildren().get(i);
                if (node instanceof PigGradingNode && PigGradingPane.this.actionMap.get(i) != null) {
                    ((PigGradingNode)node).setActionProperty((PigGradingAction)PigGradingPane.this.actionMap.get(i));
                }

                node.prefWidth(width);
                node.prefWidth(height);
                if (i % PigGradingPane.this.columnCount.get() == 0) {
                    colIndex = 0;
                }

                node.resize(width, height);
                node.relocate((double)PigGradingPane.this.hgap.get() + (double)(colIndex++) * (width + (double)PigGradingPane.this.hgap.get()), (double)PigGradingPane.this.vgap.get() + (double)rowIndex * (height + (double)PigGradingPane.this.vgap.get()));
            }

        }
    };

    public PigGradingPane() {
        this.getChildren().add(this.backPane);
        this.getChildren().add(this.contentPane);
        this.backPane.prefWidthProperty().bind(this.prefWidthProperty());
        this.backPane.prefHeightProperty().bind(this.prefHeightProperty());
        this.contentPane.prefWidthProperty().bind(this.prefWidthProperty());
        this.contentPane.prefHeightProperty().bind(this.prefHeightProperty());
    }

    public void add(Region region) {
        PigGradingNode targetNode = (PigGradingNode)region;
        if (targetNode.isEmpty()) {
            if (this.contentPane.getChildren().size() <= 0 || ((PigGradingNode)this.contentPane.getChildren().get(0)).isEmpty()) {
                return;
            }

            this.addImpl(region);
        } else if (this.contentPane.getChildren().size() <= 0) {
            this.addImpl(region);
        } else {
            PigGradingNode node0 = (PigGradingNode)this.contentPane.getChildren().get(0);
            if (node0.isEmpty()) {
                this.contentPane.getChildren().set(0, targetNode);
            } else {
                this.addImpl(region);
            }
        }

    }

    private void addImpl(Region region) {
        this.contentPane.getChildren().add(0, region);

        for(int i = 0; i < this.contentPane.getChildren().size(); ++i) {
            Node node = (Node)this.contentPane.getChildren().get(i);
            if (node instanceof PigGradingNode) {
                PigGradingNode pigGradingNode = (PigGradingNode)node;
                if (this.actionMap.get(i) == null) {
                    pigGradingNode.actionPropertyProperty().set(null);
                } else {
                    pigGradingNode.setActionProperty((PigGradingAction)this.actionMap.get(i));
                    BizEvent bizEvent = new BizEvent(((PigGradingAction)this.actionMap.get(i)).getActionCode(), pigGradingNode);
                    ApplicationContext.postEvent(bizEvent);
                }
            }
        }

        if (this.contentPane.getChildren().size() > this.columnCount.get()) {
            Node node = (Node)this.contentPane.getChildren().remove(this.contentPane.getChildren().size() - 1);
            if (node instanceof PigGradingNode) {
                PigGradingNode pigGradingNode = (PigGradingNode)node;
                pigGradingNode.setActionProperty((PigGradingAction)null);
                if (pigGradingNode.isChoiseProperty()) {
                    pigGradingNode.setChoiseProperty(false);
                    this.setSelectNode((PigGradingNode)null);
                }
            }
        }

    }

    public void push(Region region) {
        this.contentPane.getChildren().add(0, region);

        for(int i = 0; i < this.contentPane.getChildren().size(); ++i) {
            Node node = (Node)this.contentPane.getChildren().get(i);
            if (node instanceof PigGradingNode) {
                PigGradingNode pigGradingNode = (PigGradingNode)node;
                if (this.actionMap.get(i) == null) {
                    pigGradingNode.actionPropertyProperty().set(null);
                } else {
                    pigGradingNode.actionPropertyProperty().set(this.actionMap.get(i));
                    BizEvent bizEvent = new BizEvent(((PigGradingAction)this.actionMap.get(i)).getActionCode(), pigGradingNode);
                    ApplicationContext.postEvent(bizEvent);
                }
            }
        }

        if (this.contentPane.getChildren().size() > this.columnCount.get()) {
            Node node = (Node)this.contentPane.getChildren().remove(this.contentPane.getChildren().size() - 1);
            if (node instanceof PigGradingNode) {
                PigGradingNode pigGradingNode = (PigGradingNode)node;
                pigGradingNode.setActionProperty((PigGradingAction)null);
                if (pigGradingNode.isChoiseProperty()) {
                    pigGradingNode.setChoiseProperty(false);
                    this.setSelectNode((PigGradingNode)null);
                }
            }
        }

    }

    protected void layoutChildren() {
        this.backPane.resize(this.getWidth(), this.getHeight());
        this.backPane.relocate(0.0, 0.0);
        this.contentPane.resize(this.getWidth(), this.getHeight());
        this.contentPane.relocate(0.0, 0.0);
    }

    public int getRowCount() {
        return this.rowCount.get();
    }

    public IntegerProperty rowCountProperty() {
        return this.rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount.set(rowCount);
    }

    public PigGradingNode getSelectNode() {
        return this.selectNode;
    }

    public void setSelectNode(PigGradingNode selectNode) {
        this.selectNode = selectNode;
    }

    public PigGradingAction getAction() {
        return this.action;
    }

    public void setAction(PigGradingAction action) {
        PigGradingNode node = new PigGradingNode();
        node.setEmpty(true);
        node.setActionProperty(action);
        this.backPane.getChildren().add(node);
        this.actionMap.put(action.getPos(), action);
    }
}
